package com.h117121.fashionshop.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.h117121.fashionshop.constant.AppConstant;
import com.h117121.fashionshop.controller.generic.GenericController;
import com.h117121.fashionshop.core.dom.Producer;
import com.h117121.fashionshop.core.dom.SimpleQuery;
import com.h117121.fashionshop.core.exception.EntityNotFoundException;
import com.h117121.fashionshop.validator.UpdateSimpleDomValidator;

@Controller
@RequestMapping("/admin/producer")
public class AdminProducerController extends GenericController {
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private UpdateSimpleDomValidator validator;

	@RequestMapping(method = RequestMethod.GET)
	protected String showPage(HttpServletRequest request,
			@ModelAttribute(value = "query") SimpleQuery query,
			@RequestParam(value = "page", required = false) String page) {
		int indexPage = 0;
		if (page != null && !page.isEmpty()) {
			try {
				indexPage = Integer.parseInt(page);
				indexPage -= 1;
				if (indexPage < 0)
					indexPage = 0;
			} catch (NumberFormatException e) {
				indexPage = 0;
			}
		}

		if (query.getKeyWord() != null && !query.getKeyWord().isEmpty()) {
			request.setAttribute("resultPage", businessService.findProducer(
					query.getKeyWord(), indexPage,
					AppConstant.ADMIN_NUM_ITEM_PER_PAGE));
			String paraURL = ((HttpServletRequest) request).getQueryString();
			if (paraURL.contains("&page=")) {
				request.setAttribute("pageURL",
						paraURL.substring(0, paraURL.indexOf("&page=")));
			} else {
				request.setAttribute("pageURL", paraURL);
			}
		} else {
			request.setAttribute("resultPage", businessService.getAllProducer(
					indexPage, AppConstant.ADMIN_NUM_ITEM_PER_PAGE));
		}

		return "AdminProducerPage";
	}

	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	protected String deleteProducer(HttpServletRequest request,
			@RequestParam(value = "pid", required = true) String id) {
		String referer = request.getHeader("Referer");
		String queryString = "";
		try {
			businessService.deleteProducer(businessService.getProducer(Long
					.parseLong(id)));
			request.setAttribute("successfulMessage", true);
		} catch (EntityNotFoundException e) {
			request.setAttribute("notfound", true);
		}
		if (referer != null) {
			if (referer.contains("?")) {
				queryString = referer.substring(referer.indexOf("?"));
			}
		}
		return "forward:/admin/producer" + queryString;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	protected String showEditProducer(
			@RequestParam(value = "pid", required = false) String id,
			ModelMap model,
			@ModelAttribute(value = "producer") Producer producer,
			BindingResult errors) {
		if (id != null) {
			try {
				model.addAttribute("producer",
						businessService.getProducer(Long.parseLong(id)));
			} catch (EntityNotFoundException e) {
				model.addAttribute("notfound", true);
			}
		} else {
			model.addAttribute("producer", new Producer());
		}
		return "AdminProducerDetailPage";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	protected String editProducer(HttpServletRequest request,
			@RequestParam(value = "pid", required = false) String id,
			@ModelAttribute(value = "producer") Producer producer,
			BindingResult errors, ModelMap model) {
		validator.validate(producer, errors);
		if (errors.hasErrors()) {
			return "AdminProducerDetailPage";
		}

		businessService.updateProducer(producer);
		model.addAttribute("successfulMessage", true);
		return "AdminProducerDetailPage";
	}
}
